using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Windows.Controls;

namespace GameForKids
{
    public enum Way
    {
        none,
        up,
        down,
        left,
        right
    }

    public class Player : DrawableGameComponent
    {
        // Animations
        private Animations idleAnimation;
        private Animations runAnimation;
        private SpriteEffects flip = SpriteEffects.None;
        //vi tri
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        Vector2 position;
        // xoay hinh
        public float Rotate
        {
            get { return rotate; }
            set { rotate = value; }
        }
        float rotate;
        // hinh de ve va huong ve
        private AnimationPlayer sprite;
        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
        Vector2 velocity;
        // toc do chay
        private const float moveSpeed = 100.0f;
        private const float maxSpeed = 5f;
        //cho phep di chuyen
        public bool canMoveLeft = true;
        public bool canMoveRight = true;
        public bool canMoveUp = true;
        public bool canMoveDown = true;
        public Way way;
        // hang so kich thuoc
        public const int WIDTH = 55;
        public const int HEIGHT = 55;
        // xet huong xoay man hinh
        int alpha = 1;
        PageOrientation orient;

        public Rectangle BoundingRectangle()
        {
            Rectangle RECT = new Rectangle((int)Position.X-28,(int)Position.Y-28, 55, 55);
            return RECT;
        }

        public Player(Game game, Vector2 p) : base(game)
        {
            Position = p;
            Rotate = 0.0f;
            loadContent();
            way = Way.none;
        }

        public void loadContent()
        {
            runAnimation = new Animations(this.Game.Content.Load<Texture2D>("Textures/PacManGame/pacman"), 0.05f, true);
            idleAnimation = new Animations(this.Game.Content.Load<Texture2D>("Textures/PacManGame/idle"), 0.1f, true);
            Velocity = Vector2.Zero;
            sprite.PlayAnimation(runAnimation);
        }

        public override void Initialize()
        {
            base.Initialize();
        }
        //
        public void resetAlowWay()
        {
                  canMoveLeft = true;
                canMoveRight = true;
                canMoveUp = true;
                canMoveDown = true;
        }
        public void fixUp()
        {
            position = Position;
            position.Y = position.Y+ 16-(((int)position.Y - 50) % 15);
            Position = position;
        }
        public void fixDown()
        {
            position = Position;
            position.Y = position.Y-1-(((int)position.Y + (HEIGHT/2)-50) % 15);
            Position = position;
        }
        public void fixLeft()
        {
            position = Position;
            position.X = position.X + 16 - (((int)position.X - 100) % 15);
            Position = position;
        }
        public void fixRight()
        {
            position = Position;
            position.X = position.X - 1- (((int)position.X+(WIDTH/2)-100)%15);
            Position = position;
        }
       public virtual void Update(GameTime gameTime)
        {
            velocity.X = PacManGame.move3.X ;
            velocity.Y = PacManGame.move3.Y ;
            Velocity = velocity;

            if (Math.Abs(Velocity.X) - 0.02f > 0 || Math.Abs(Velocity.Y) - 0.02f > 0)
            {
                sprite.PlayAnimation(runAnimation);
            }
            else
            {
                way = Way.none;
                sprite.PlayAnimation(idleAnimation);
            }


            CheckOrientation(orient);

            // lat hinh neu doi huong chay
            if (Math.Abs(Velocity.X) > Math.Abs(Velocity.Y))
            {
                if (Velocity.X > 0 && canMoveUp)
                {
                    if (Velocity.X > 0.02f)
                    {
                        way = Way.up;
                        canMoveDown = true;
                        position = Position;
               
                        if(Math.Abs(Velocity.X * moveSpeed)<maxSpeed)
                             position.Y -= (Velocity.X * moveSpeed)*alpha;
                        else
                            position.Y -= maxSpeed*alpha;

                    }
                        Position = position;

                    flip = SpriteEffects.None;
                    Rotate = -(float)Math.PI / 2;
                }
                else if (Velocity.X < 0 && canMoveDown)
                {
                    if (Velocity.X < -0.02f)
                    {
                        way = Way.down;
                        canMoveUp = true;
                        position = Position;
                        if (Math.Abs(Velocity.X) * moveSpeed < maxSpeed)
                            position.Y += (Math.Abs(Velocity.X) * moveSpeed)*alpha;
                        else
                            position.Y += maxSpeed*alpha;
                    }
                    Position = position;
                    flip = SpriteEffects.None;
                    Rotate = (float)Math.PI / 2;
                }
            }
            else if (Math.Abs(Velocity.X) < Math.Abs(Velocity.Y))
            {
                if (Velocity.Y > 0 && canMoveLeft)
                {
                    if (Velocity.Y > 0.02f)
                    {
                        way = Way.left;
                        canMoveRight = true;
                        position = Position;
                        if (Math.Abs(Velocity.Y * moveSpeed) < maxSpeed)
                            position.X -= Velocity.Y * moveSpeed*alpha;
                        else
                            position.X -= maxSpeed*alpha;
                    }
                    Position = position;
                    flip = SpriteEffects.FlipHorizontally;
                    Rotate = 0.0f;
                }
                else if (Velocity.Y < 0 && canMoveRight)
                {
                    if (Velocity.Y < -0.02f)
                    {
                        way = Way.right;
                        canMoveLeft = true;
                        position = Position;
                        if (Math.Abs(Velocity.Y) * moveSpeed < maxSpeed)
                            position.X += Math.Abs(Velocity.Y) * moveSpeed*alpha;
                        else
                            position.X += maxSpeed*alpha;
                    }
                    Position = position;
                    flip = SpriteEffects.None;
                    Rotate = 0.0f;
                }
            }
            base.Update(gameTime);
        }
       private void CheckOrientation(PageOrientation orient)
       {
           if (orient == PageOrientation.LandscapeLeft)
           {
               alpha = -1;
           }
           else if (orient == PageOrientation.LandscapeRight)
           {
               alpha = 1;
           }
 
       }
       public virtual void Draw(GameTime gametime, SpriteBatch spriteBatch)
       {
         
           sprite.Draw(gametime, spriteBatch, Position, flip,Rotate);
       }
   
    }
}
